﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.ServiceModel.Web;

namespace Core
{
    public static class StreamHelper
    {
        public static T ToEntity<T>(this Stream stream)
        {
            StreamReader reader = null;
            try
            {
                reader = new StreamReader(stream, System.Text.Encoding.UTF8);
                string json = reader.ReadToEnd();
                reader.Close();
                reader.Dispose();
                return JsonConvert.DeserializeObject<T>(json);
            }
            catch (Exception ex)
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                Log.errorlog(ex);
                return default(T);
            }
        }

        public static string ToJsonResult<T>(RtnObj<T> rtnObj)
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin:*");
            return JsonConvert.SerializeObject(rtnObj);
        }
    }
    public class RtnObj<T>
    {
        public bool sucess { get; set; }
        public int code { get; set; }
        public T data { get; set; }
        public string message { get; set; }
    }
}
